//
// Created by Administrator on 2017/4/11.
//

#include <jni.h>

#include <android/log.h>
#define LOG_TAG "clog"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

extern "C" {
/*
 * Class:     com_jacky_me_jniapp_ccallj_CLoadJMethod
 * Method:    callMethod1
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_jacky_me_jnidemo_calljni_CLoadJMethod_callMethod1
        (JNIEnv *env, jobject _jobject) {

    /*
     *
        Class<?> forName = Class.forName("com.jacky.me.jnidemo.calljni.CLoadJMethod");
        Method declaredMethod = forName.getDeclaredMethod("helloFromJava", new Class[]{});
        declaredMethod.invoke(forName.newInstance(), new Object[]{});
     *
     *
     */
    ///jclass      (*FindClass)(JNIEnv*, const char*);
    jclass clazz = env->FindClass("com/jacky/me/jnidemo/calljni/CLoadJMethod");
    //  jmethodID   (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
    // 方法签名  参数和返回值
    jmethodID methodId = env->GetMethodID(clazz, "helloFromJava", "()V");
    // void        (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
    env->CallVoidMethod(_jobject, methodId);
     // 设置种子
}

/*
 * Class:     com_jacky_me_jnidemo_calljni_CLoadJMethod
 * Method:    callMethod2
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_jacky_me_jnidemo_calljni_CLoadJMethod_callMethod2
        (JNIEnv *env, jobject _jobject) {
    jclass clazz = env->FindClass("com/jacky/me/jnidemo/calljni/CLoadJMethod");
    jmethodID methodId = env->GetMethodID(clazz, "Add", "(II)I");
    // jint        (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...);
    env->CallIntMethod(_jobject, methodId, 3, 5);
}

/*
 * Class:     com_jacky_me_jnidemo_calljni_CLoadJMethod
 * Method:    callMethod3
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_jacky_me_jnidemo_calljni_CLoadJMethod_callMethod3
        (JNIEnv *env, jobject _jobject) {   // 参数 object  就是native方法所在的类
    jclass clazz = env->FindClass("com/jacky/me/jnidemo/calljni/CLoadJMethod");
    jmethodID methodId = env->GetMethodID(clazz, "printString", "(Ljava/lang/String;)V");
    // jint        (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...);
    jstring str = env->NewStringUTF("hello");

    env->CallVoidMethod(_jobject, methodId, str);

}

/*
 * Class:     com_jacky_me_jnidemo_calljni_CLoadJMethod
 * Method:    callMethod4
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_jacky_me_jnidemo_calljni_CLoadJMethod_callMethod4
        (JNIEnv *env, jobject) {
    jclass clazz = env->FindClass("com/jacky/me/jnidemo/calljni/CLoadJMethod");
    //  jmethodID   (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
    // 方法签名  参数和返回值
    jmethodID methodId = env->GetMethodID(clazz, "helloFromJava", "()V");
    // void        (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
    // 需要创建CLoadJMethod的 对象
    // jobject     (*AllocObject)(JNIEnv*, jclass);
    jobject obj = env->AllocObject(clazz);  // new MainActivity();
    env->CallVoidMethod(obj, methodId);

}

/*
 * Class:     com_jacky_me_jnidemo_calljni_CLoadJMethod
 * Method:    callMethod5
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_jacky_me_jnidemo_calljni_CLoadJMethod_callMethod5
        (JNIEnv *env, jobject) {
    jclass clazz = env->FindClass("com/jacky/me/jnidemo/calljni/CLoadJMethod");
    //     jmethodID   (*GetStaticMethodID)(JNIEnv*, jclass, const char*, const char*);
    jmethodID methodid = env->GetStaticMethodID(clazz, "demo", "()V");
    //void        (*CallStaticVoidMethod)(JNIEnv*, jclass, jmethodID, ...);
    env->CallStaticVoidMethod(clazz, methodid);
}


}